package sample;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DbTool {
    static {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 执行update SQL
     * @param path  mdb文件路径
     * @param sql  udpate sql
     * @return
     */
    public static int exeUpdateSql(String path,String sql) {
        Connection connection = null;
        Statement statement = null;
        int result = 0;
        try {
            //String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\test.mdb";
            String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + path;
            connection = DriverManager.getConnection(url);
            statement = connection.createStatement();
//          int result = statement.executeUpdate("update yw set tm=qfywbf");
            result = statement.executeUpdate(sql);
            return result;
        } catch (SQLException e) {
            SBTFxDialog.showConfirmDialog(null, e.getMessage(), "【ERROR】");
        } finally {
            try {
                if (null != statement) {
                    statement.close();
                }
                if (null != connection) {
                    connection.close();
                }
            } catch (SQLException e) {
                SBTFxDialog.showConfirmDialog(null, e.getMessage(), "【ERROR】");
            }
        }
        return result;
    }
}
